package Code.Objects.Effects.Visual.GunfireBursts;

import org.jrabbit.standard.game.graphics.skins.animation.AnimatedSkin;
import org.jrabbit.standard.game.main.world.World;
import org.jrabbit.standard.game.objects.base.Spatial;
import org.jrabbit.standard.game.objects.specialized.AnimatedSprite;

public class GunfireBurst extends AnimatedSprite
{
	protected int duration;
	protected int timer;
	protected Spatial parent = null;
	protected float offset;
	protected World world;

	public GunfireBurst(AnimatedSkin skin, Spatial gun, float distance, World w)
	{
		super(skin);
		setup(gun, distance);
		world = w;
	}

	protected void setup(Spatial gun, float distance)
	{
		duration = 10000;
		timer = 0;
		parent = gun;
		offset = distance;
		adjust();
	}
	
	private void adjust()
	{
		if(parent != null)
		{
			location.set(parent.location());
			rotation.set(parent.rotation());
			location.addPolar(offset, rotation.theta());
		}
	}
	
	public void update(int delta)
	{
		super.update(delta);
		timer += delta;
		if(timer >= duration)
		{
			kill();
		}
		adjust();
	}
	
	public void onCycleEnd(int cycle)
	{
		kill();
	}

	public void kill()
	{
		world.removeRenderable(this);
		world.removeUpdateable(this);
	}
}